<template>
  <div class="app-container">
    <div v-show="type == 0">
      <el-row>
        <el-col :span="24">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker clearable style="width:170px" v-model="queryParams.startTime" type="date"
                value-format="yyyy-MM-dd" placeholder="请选择开始时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker clearable style="width:170px" v-model="queryParams.endTime" type="date"
                value-format="yyyy-MM-dd" placeholder="请选择结束时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
          <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table style="margin-left:10px" size="mini" height="400" :row-style="{ height: '30px' }" border
            :cell-style="{ padding: '0px' }" v-loading="loading" :data="projectInfoList" row-key="id"
            :tree-props="{ children: 'children' }" efault-expand-all>
            <el-table-column label="序号" type="index" align="center" prop="index" :index="indexMethod"></el-table-column>
            <el-table-column label="项目编号" align="center" prop="projectNumber" width="100"
              :show-overflow-tooltip="true" />
            <el-table-column label="项目名称" align="center" prop="projectName" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <!-- 使用按钮作为可点击元素，并绑定点击事件 -->
                <el-button size="small" @click="handleClick(scope.row, scope.$index + 1)" type="text">
                  {{ scope.row.projectName }}
                </el-button>
              </template>
            </el-table-column>
            <el-table-column label="项目负责人" align="center" prop="projectLeader" width="110"
              :show-overflow-tooltip="true" />
            <el-table-column label="计划开始时间" align="center" prop="startTime" width="95">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="计划结束时间" align="center" prop="endTime" width="95">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="项目执行人" align="center" prop="executorName" width="110"
              :show-overflow-tooltip="true" />
            <el-table-column label="状态" align="center" prop="state" width="60">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.project_status" :value="scope.row.state" />
              </template>
            </el-table-column>
            <el-table-column label="创建人" align="center" prop="creationName" width="80" :show-overflow-tooltip="true" />
            <el-table-column label="创建时间" align="center" prop="creationTime" width="100">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.creationTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="123" class-name="small-padding fixed-width">
              <template slot-scope="scope" v-if="scope.row.executorId == userId || scope.row.projectLeaderId == userId">
                <el-button size="mini" type="text" icon="el-icon-s-operation"
                  @click="handleJD(scope.row, scope.$index + 1)" title="进度" v-if="scope.row.state != 4 && scope.row.state != 5"></el-button>
                <el-button v-if="scope.row.sign == '主'" size="mini" type="text" icon="el-icon-s-order"
                  @click="handleChange(scope.row, scope.$index + 1)" title="变更" ></el-button>
                <el-button size="mini" type="text" icon="el-icon-circle-check" @click="handleOK(scope.row)" title="完成" v-if="scope.row.state != 4 && scope.row.state != 5"></el-button>
                <el-button size="mini" type="text" icon="el-icon-error" @click="handleStop(scope.row)" title="终止" v-if="scope.row.state != 4 && scope.row.state != 5"></el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-col>
      </el-row>
    </div>
    <!-- 进度 -->
    <div v-if="type == 1">
      <Operate @change="close" :opt="edit" :id="id" :sign="sign" :index="index" :addShow="addShow"></Operate>
    </div>

    <!-- 主项目变更 -->
    <div v-if="type == 2">
      <Change @change="close" :opt="edit" :id="id" :sign="sign" :index="index"></Change>
    </div>

    <!-- 子项目变更 -->
    <div v-if="type == 3">
      <SubChange @change="close" :opt="edit" :id="id" :sign="sign" :index="index"></SubChange>
    </div>
  </div>
</template>

<script>
import { listProjectInfo, listProjectTree, getProjectInfo, delProjectInfo, addProjectInfo, editMainClass, updateProjectInfo } from "@/api/project/projectInfo";
import { getScheduleName,finishProject } from "@/api/project/projectSchedule";
import MainType from "@/components/MainType/indexNew";
import { getToken } from "../../../utils/auth";
import Operate from "./operate.vue";
import Change from "./change.vue";
import SubChange from "./subChange.vue";
import { listType } from "@/api/maintype/type";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { MainType, Operate, Treeselect, Change },
  dicts: ['project_status'],
  name: "projectSchedule",
  data() {
    return {
      userId: "",
      typeCode: 'project',
      type: 0,
      opt: 'add',
      id: '',
      sign: '',
      obj: null,
      index: null,
      edit: 'edit',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目信息表格数据
      projectInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        state: null,
        projectName: null,
        startTime: null,
        endTime: null,
        attackTime: null,
        completeTime: null,
      },
      queryParamsZ: {
        type: null,

      },
      queryParamsSchedule: {
        projectId: null,
        state: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      addShow: true
    };
  },
  created() {
    this.getList();
    this.userId = this.$store.state.user.info.userId
  },
  methods: {
    indexMethod(index) {
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
    },
    //树点击事件
    getNode(data, node, obj) {
      this.queryParams.classificationId = data.id
      this.getList()
    },
    /** 查询项目信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.state = 2//只查询实施状态的项目信息
      listProjectTree(this.queryParams, this.queryParamsZ).then(response => {
        this.projectInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    close(data) {
      this.type = 0
      this.getList();
      this.open = data;

    },
    //项目名称跳转
    handleClick(row, index) {
      this.edit = "details"
      this.reset();
      this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index
      this.id = row.id;
      this.type = 1
      this.sign = row.sign;
      //需要判断项目阶段是否全部完结
      this.queryParamsSchedule.projectId = row.id
      this.queryParamsSchedule.state = "0"//查询未完成的阶段
      getScheduleName(this.queryParamsSchedule).then(response1 => {
        if (response1.data.length == 0) {
          this.addShow = false
        } else {
          this.addShow = true
        }
      })
      //this.reset();
    },
    /** 进度按钮操作 */
    handleJD(row, index) {
      this.reset();
      this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index
      this.edit = "edit"
      this.id = row.id;
      this.sign = row.sign;
      this.type = 1
      //需要判断项目阶段是否全部完结
      this.queryParamsSchedule.projectId = row.id
      this.queryParamsSchedule.state = "0"//查询未完成的阶段
      getScheduleName(this.queryParamsSchedule).then(response1 => {
        if (response1.data.length == 0) {
          this.addShow = false
        } else {
          this.addShow = true
        }
      })
    },
    /** 变更按钮操作 */
    handleChange(row, index) {
      this.reset();
      this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index
      this.edit = "edit"
      this.id = row.id;
      this.type = 2
      // if(row.sign=="主"){
      //   this.type = 2
      // }else if(row.sign=="子"){
      //   this.type = 3
      // }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        classificationId: null,
        projectNumber: null,
        projectName: null,
        contractId: null,
        contractName: null,
        projectLeaderId: null,
        projectLeader: null,
        executorId: null,
        executorName: null,
        startTime: null,
        endTime: null,
        attackTime: null,
        completeTime: null,
        state: null,
        operateTime: null,
        operateId: null,
        operateName: null,
        departmentId: null,
        departmentName: null,
        organizationId: null,
        organizationName: null,
        creationId: null,
        creationName: null,
        creationTime: null,
        belongTo: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const projectId = row.projectId || this.ids
      getProjectInfo(projectId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目信息";
      });
    },
    /** 完成按钮操作 */
    handleOK(row) {
      //需要判断项目阶段是否全部完结
      this.queryParamsSchedule.projectId = row.id
      this.queryParamsSchedule.state = "0"//查询未完成的阶段
      if (row.sign == "子") {
        //完结
        this.$modal.confirm('确认后不允许再修改，是否确认完成子项目名称为"' + row.projectName + '"的数据项？').then(function () {
        }).then(() => {
          finishProject(row.id,"4").then(response => {
            this.getList();
            this.$modal.msgSuccess("操作成功");
          });
        }).catch(() => { });
      }else {
        getScheduleName(this.queryParamsSchedule).then(response1 => {
          if (response1.data.length == 0) {
            this.form.projectId = row.id
            this.form.state = 3
            this.$modal.confirm('确认后不允许再修改，是否确认完成项目名称为"' + row.projectName + '"的数据项？').then(function () {
            }).then(() => {
              updateProjectInfo(this.form).then(response => {
                this.getList();
                this.$modal.msgSuccess("操作成功");
              });

            }).catch(() => { });
          } else {
            this.$message.error('请先完成所有项目阶段！');
            return;
          }
        })
      }



    },
    /** 终止按钮操作 */
    handleStop(row) {
      this.form.projectId = row.id
      this.form.state = 5
      if (row.sign == "子") {
        //完结
        this.$modal.confirm('确认后不允许再修改，是否确认终结子项目名称为"' + row.projectName + '"的数据项？').then(function () {
        }).then(() => {
          finishProject(row.id,"5").then(response => {
            this.getList();
            this.$modal.msgSuccess("操作成功");
          });
        }).catch(() => { });
      }else {
        this.$modal.confirm('终止后该项目作废不能再修改，是否确认终止项目名称为"' + row.projectName + '"的数据项？').then(function () {
        }).then(() => {
          updateProjectInfo(this.form).then(response => {
            this.getList();
            this.$modal.msgSuccess("终止成功");
          });

        }).catch(() => { });
      }

    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const projectIds = row.projectId || this.ids;
      this.$modal.confirm('是否确认删除项目信息编号为"' + projectIds + '"的数据项？').then(function () {
        return delProjectInfo(projectIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/projectInfo/export', {
        ...this.queryParams
      }, `projectInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
